<template>
	<view class="" :class="ggclass">
		<view class="select-item" @click.capture="spread" :style="{padding:`0 ${padd}rpx`,width,height}">
			<view class="item" v-if="name">
				<slot></slot><text>{{name}}</text>
				<text class="iconfont icon-xiajiantou mg_l10"
					:style="{transform: `rotate(${num}deg)`,transition: '0.5s'}">
				</text>
			</view>
			<view class="item" v-else>
				<slot></slot><text class="grey">{{hint}}</text>
				<text class="iconfont icon-xiajiantou mg_l10"
					:style="{transform: `rotate(${num}deg)`,transition: '0.5s'}">
				</text>
			</view>
		</view>

		<uni-popup ref="popRef" :round="50" :safe-area="false" @change="changePop">
			<view class="picker-view-box">
				<view class="picker-title">
					<text class="" @click="close">取消</text>
					<view class="font_s32 font-weight">{{title}}</view>
					<text class="blue" @click="submit">确定 </text>
				</view>
				<view class="date-box mg_t40">
					<view class="date" :class="{pitch:index===0}" @click="cut(0)">
						{{startDate |dateFilter}}
					</view>
					<text>至</text>
					<view class="date" :class="{pitch:index===1}" @click="cut(1)">
						{{ endDate|dateFilter}}
					</view>
				</view>
				<picker-view v-if="visible" :indicator-style="indicatorStyle" :value="values" @change="bindChange"
					class="picker-view">
					<picker-view-column>
						<view class="item" v-for="(item,index) in years" :key="index">{{item}}年</view>
					</picker-view-column>
					<picker-view-column>
						<view class="item" v-for="(item,index) in months" :key="index">{{item}}月</view>
					</picker-view-column>
					<picker-view-column>
						<view class="item" v-for="(item,index) in days" :key="index">{{item}}日</view>
					</picker-view-column>
				</picker-view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		dateFilter as filter
	} from '../ways/filter.js'
	const date = new Date()
	export default {
		name: "c-picker-start-end",
		inject: {
			Cinput: {
				default: {}
			}
		},
		props: {
			title: {
				type: String,
				default: '自定义'
			},
			disabled: {
				type: Boolean,
				default: false
			},
			value: {
				type: Array,
				default: () => [2022, date.getMonth(), date.getDate() - 1]
			},
			range_key: {
				type: String,
				default: 'name'
			},
			range_value: {
				type: String,
				default: 'id'
			},
			immediate: {
				type: Boolean,
				default: false
			},
			hint: {
				type: String,
				default: '时间筛选'
			}
		},
		data() {
			return {
				visible: false,
				years: [],
				year: date.getFullYear(),
				months: [],
				month: date.getMonth()+1,
				days: [],
				day: date.getDate(),
				values: [],
				indicatorStyle: `height: 50px;`,
				index: 0,
				startDate: date,
				endDate: date,
				name: '',
				num: 0,
			}
		},
		mounted() {
			for (let i = 1990; i <= date.getFullYear(); i++) {
				this.years.push(i)
			}
			this.months = this.getMonths(this.year)
			this.days = this.getDate(this.year, this.month)
			this.values = [...this.value]
		},
		methods: {
			changePop(e){
				console.log(e,'eeee')
				if(e.show){
					this.num = 180
				}else{
					this.num = 0
				}
			},
			close() {
				this.$refs.popRef.close()
				this.visible = false
			},
			spread() {
				if (this.disabled) return
				this.$refs.popRef.open('bottom')
				this.visible = true
				this.setValues(this.index)
			},
			cut(e) {
				this.index = e
				this.setValues(this.index)
			},
			getMonths(year) {
				let months = []
				let num = 0
				if (year == this.year) {
					num = date.getMonth() + 1
				} else {
					num = 12
				}
				for (let i = 1; i <= num; i++) {
					months.push(i)
				}
				return months
			},
			getDate(year, month) {
				let days = []
				let num = 0
				if (year == this.year && month == this.month) {
					num = date.getDate()
				} else {
					num = new Date(year, month, 0).getDate()
				}
				for (let i = 1; i <= num; i++) {
					days.push(i)
				}
				return days
			},
			show() {
				this.$refs.popRef.open('bottom')
				this.visible = true
				this.setValues(this.index)
			},
			setValues(index) {
				let defaultValues = []
				if (index) {
					defaultValues = filter(this.endDate).split('-')
				} else {
					defaultValues = filter(this.startDate).split('-')
				}
				this.values = [
					this.years.findIndex(v => v == defaultValues[0]),
					this.months.findIndex(v => v == defaultValues[1]),
					this.days.findIndex(v => v == defaultValues[2])
				]
			},
			bindChange(e) {
				const val = e.detail.value
				let year = this.years.find((v, i) => val[0] == i)
				let month = this.months.find((v, i) => val[1] == i)
				let day = this.days.find((v, i) => val[2] == i)
				this.months = this.getMonths(year)
				this.days = this.getDate(year, month - 1)
				let data = [year, month, day].join('/')
				if (this.index) {
					this.endDate = new Date(data)
				} else {
					this.startDate = new Date(data)
				}
			},
			submit() {
				this.close()
				uni.showLoading()
				let time = setTimeout(() => {
					let params = {
						startTime: filter(this.startDate, 'yyyy/MM/dd'),
						endTime: filter(this.endDate, 'yyyy/MM/dd')
					}
					this.$emit('change', params)
					this.$emit('input', params)
					this.name = `${params.startTime}-${params.endTime}`
					uni.hideLoading()
					clearTimeout(time)
					this.$nextTick(() => {
						typeof this.Cinput.verify === 'function' && this.Cinput.verify()
					})
				}, 1000)
			},
		}
	}
</script>
<style lang="scss" scoped>
	.picker-view-box {
		position: relative;
		z-index: 1000;
		background-color: #fff;
		border-radius: 32rpx 32rpx 0 0;
	}

	.picker-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
	}

	.picker-view {
		width: 750rpx;
		height: 500rpx;
		// margin-top: 20rpx;
	}

	.item {
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		text-align: center;
	}

	.date-box {
		display: flex;
		justify-content: space-between;
		padding: 0 80rpx;

		.date {
			font-size: 36rpx;
			color: #333333;
			border-bottom: 2rpx #cecece solid;
			padding-bottom: 10rpx;
		}

		&>.pitch {
			color: $uni-color-main;
			border-bottom: 2rpx solid $uni-color-main;
		}
	}

	.select-item {
		// height: 70rpx;
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-radius: 10rpx;
	}

	.icon-xiajiantou {
		display: inline-block;
	}
</style>